package com.kangaroo.myhome.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.kangaroo.myhome.mapper.TInspSubwayMapper;
import com.kangaroo.myhome.model.*;
import com.kangaroo.myhome.utils.SecurityUtils;
import com.kangaroo.myhome.vo.Option;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kangaroo.myhome.mapper.TSysUserMapper;
import com.kangaroo.myhome.vo.ResponseModel;

import tk.mybatis.mapper.util.StringUtil;

@Service
public class SubWayService {

    @Autowired
    private TInspSubwayMapper tInspSubwayMapper;

    /**
     * 
     * @param name
     * @param page
     * @param rows
     * @return
     */
    public ResponseModel findAll(String name, int page, int rows) {
        ResponseModel model = new ResponseModel();

        CommonSearch search = new CommonSearch();
        search.setString1(name);
        search.setPagination(page, rows);
        List<TInspSubway> users = tInspSubwayMapper.selectByPagination(search);
        long count = tInspSubwayMapper.selectByPaginationCount(search);
        model.setCode(200);
        model.setMessage("数据获取成功");
        model.setDataDataGrid(users, count);
        return model;
    }

    public ResponseModel findSelect() {
        ResponseModel model = new ResponseModel();
        List<TInspSubway> users = tInspSubwayMapper.findSelect();
        List<Option> options = new ArrayList<Option>();

        for(TInspSubway subway:users){
            Option option = new Option(subway.getId(),subway.getName());
            options.add(option);
        }
        model.setCode(200);
        model.setMessage("数据获取成功");
        model.setData(options);
        return model;
    }

    /**
     * @param tInspSubway
     * @return
     */
    public ResponseModel edit(TInspSubway tInspSubway) {

        ResponseModel model = new ResponseModel();

        if (tInspSubway == null) {
            model.setCode(201);
            model.setMessage("参数异常");
            return model;
        }

        int result = 0;
        String id = tInspSubway.getId();
        if (StringUtil.isEmpty(id)) {
            TInspSubway subWayExist = view(tInspSubway);
            if (subWayExist != null) {
                model.setCode(201);
                model.setMessage("地铁：【" + tInspSubway.getName() + "】已经存在！");
                return model;
            } else {
                tInspSubway.setCreateTime(new Date());
                tInspSubway.setId(UUID.randomUUID().toString());
                tInspSubway.setCreator(SecurityUtils.getUsername());

                result = tInspSubwayMapper.insertSelective(tInspSubway);
            }

        } else {
            result = tInspSubwayMapper.updateByPrimaryKeySelective(tInspSubway);
        }

        model.setCode(200);
        model.setMessage("操作成功");
        return model;
    }

    /**
     * @param tInspSubway
     * @return
     */
    public TInspSubway view(TInspSubway tInspSubway) {
        ResponseModel model = new ResponseModel();

        TInspSubwayExample example = new TInspSubwayExample();
        String id = tInspSubway.getId();
        String name = tInspSubway.getName();
        TInspSubway resultSubWay = null;
        if (StringUtil.isNotEmpty(id)) {
            resultSubWay = tInspSubwayMapper.selectByPrimaryKey(id);
            return resultSubWay;
        }
        if (StringUtil.isNotEmpty(name)) {
            example.createCriteria().andNameEqualTo(name);
            List<TInspSubway> users = tInspSubwayMapper.selectByExample(example);
            if (users != null && users.size() > 0) {
                resultSubWay = users.get(0);
            }
        }

        return resultSubWay;
    }

    public void delete(TInspSubway tInspSubway) {
        String ids = tInspSubway.getId();
        String[] idArr = ids.split(",");
        for(String id : idArr) {
            tInspSubwayMapper.deleteByPrimaryKey(id);
        }
    }

}
